home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacGS 2.5.2ß3 / (MacGSLib.π) / gdevmacpr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-17  |  10.1 KB  |  562 lines  |  [TEXT/R*ch]

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmacpr.c */
  21. /* (color) Macintosh printer driver for Ghostscript library */
  22.  
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <Icons.h>
  26. #include "gdevmacpr.h"
  27. #include "gserrors.h"
  28. #include "gp_macui.h"
  29.  
  30.  
  31. static void DoInit            (MenuHandle hGUIMenu, short menuOffset);
  32. static void DoMenu            (short menuItem);
  33. static void DoKeyEvent        (EventRecord *pEvent);
  34. static void DoConsolePuts    (void);
  35. static void DoOpenFilePre    (void);
  36. static void DoOpenFilePost    (void);
  37. static char DoOptionsProc    (short actionValue);
  38. static void DoTerminate        (void);
  39.  
  40.  
  41. static GUIProcRec macprGUIProcs =
  42. {
  43.     (GUIInit) NULL,            //    DoInit,
  44.     (GUINoArgFunc) NULL,    //    DoCursor,
  45.     (GUINoArgFunc) NULL,    //    DoCut,
  46.     (GUINoArgFunc) NULL,    //    DoCopy,
  47.     (GUINoArgFunc) NULL,    //    DoPaste
  48.     (GUINoArgFunc) NULL,    //    DoClear,
  49.     DoMenu,
  50.     (GUIActivate) NULL,        //    DoActivate,
  51.     (GUIUpdate) NULL,        //    DoUpdate,
  52.     (GUIEvent) NULL,        //    DoMouseDown,
  53.     DoKeyEvent,
  54.     (GUINoArgFunc) NULL,    //    DoConsolePuts,
  55.     DoOpenFilePre,
  56.     DoOpenFilePost,
  57.     DoOptionsProc,
  58.     DoTerminate
  59. };
  60.  
  61.  
  62. #define DEFAULT_DPI 72    /* Macintosh standard monitor */
  63. #define DEFAULT_WIDTH  ( 9 * DEFAULT_DPI)
  64. #define DEFAULT_HEIGHT (12 * DEFAULT_DPI)
  65.  
  66.  
  67. /* The device descriptor */
  68.  
  69. private gx_device_procs macpr_procs =
  70. {
  71.     macpr_open,
  72.     macpr_get_initial_matrix,
  73.     macpr_sync,
  74.     macpr_output_page,
  75.     macpr_close,
  76.     macpr_map_rgb_color,
  77.     macpr_map_color_rgb,
  78.     macpr_fill_rectangle,
  79.     gx_default_tile_rectangle,
  80.     macpr_copy_mono,
  81.     macpr_copy_color,
  82.     macpr_draw_line,
  83.     gx_default_get_bits,
  84.     gx_default_get_props,
  85.     gx_default_put_props
  86. };
  87.  
  88.  
  89. /* The instance is public. */
  90.  
  91. gx_device_macpr gs_macpr_device =
  92. {
  93.     sizeof (gx_device_macpr),
  94.     &macpr_procs,
  95.     "macpr",
  96.      DEFAULT_WIDTH, DEFAULT_HEIGHT,    /* x and y extent */
  97.      DEFAULT_DPI  , DEFAULT_DPI   ,    /* x and y density */
  98.     no_margins,
  99.     dci_black_and_white,            /* # of bits per pixel,
  100.                                      * # of distinct color levels - 1,
  101.                                      * size of color cube for dithering */
  102.      0,            /* connection not initialized */
  103.  
  104.     GUI_MAGIC_NUMBER,
  105.     &macprGUIProcs,
  106.  
  107.     (CWindowPtr) NULL
  108. };
  109.  
  110. /* Macros to extract colors */
  111.  
  112. #define RED(x)        ((x & 0x00FF0000) >> 8)
  113. #define GREEN(x)    ((x & 0x0000FF00))
  114. #define BLUE(x)        ((x & 0x000000FF) << 8)
  115.  
  116. /* Macro to validate arguments */
  117.  
  118. #define check_rect()                            \
  119. {                                                \
  120.     if (w <= 0 || h <= 0)                        \
  121.         return 0;                                \
  122.                                                 \
  123.     if (x < 0 || x > xdev->width  - w ||        \
  124.         y < 0 || y > xdev->height - h)            \
  125.         return_error (gs_error_rangecheck);        \
  126. }
  127.  
  128.  
  129. #define BEGIN_DRAW_PRIVATE(theEnd)                \
  130. {                                                \
  131.     CWindowPtr    pictWin = macPictWindow (dev);    \
  132.     void     macBeginPictDraw (gx_device *dev);    \
  133.     short     macEndPictDraw   (gx_device *dev);    \
  134.                                                 \
  135.                                                 \
  136.     {                                            \
  137.         CWindowPtr windowPtr = pictWin;            \
  138.                                                    \
  139.                                                    \
  140.         macBeginPictDraw (dev);                    \
  141.                                                 \
  142.         {
  143.  
  144. #define END_DRAW_PRIVATE                        \
  145.         }                                        \
  146.                                                 \
  147.         retVal = macEndPictDraw (dev);            \
  148.     }                                            \
  149. }
  150.  
  151.  
  152. #define BEGIN_DRAW                                \
  153.     BEGIN_DRAW_PRIVATE (2)
  154.  
  155. #define END_DRAW                                \
  156.     END_DRAW_PRIVATE
  157.  
  158.  
  159. #define BEGIN_DRAW_SPECIAL(fClearPicture)        \
  160.     BEGIN_DRAW_PRIVATE (fClearPicture ? 1 : 2)
  161.  
  162.  
  163. #define END_DRAW_SPECIAL                        \
  164.     END_DRAW
  165.  
  166.  
  167. short    macCreateWindow (gx_device *dev, short defaultWidth, short defaultHeight);
  168. short    macCloseWindow  (gx_device *dev);
  169. void    macClosePicture (gx_device *dev);
  170. void    macClearPicture (gx_device *dev);
  171.  
  172.  
  173.     private int
  174. macpr_open (register gx_device *dev)
  175.  
  176. {
  177.     return macCreateWindow (dev, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  178. }
  179.  
  180.  
  181. /* Close the device. */
  182.  
  183.     private int
  184. macpr_close (register gx_device *dev)
  185.  
  186. {
  187.     int        retVal = macCloseWindow (dev);
  188.  
  189.  
  190.     if (retVal == 0)
  191.     {
  192.     }
  193.  
  194.     return retVal;
  195. }
  196.  
  197.  
  198. /* Synchronize the display with the commands already given */
  199.  
  200.     private int
  201. macpr_sync (register gx_device *dev)
  202.  
  203. {
  204.     return 0;
  205. }
  206.  
  207.  
  208. /* Fill a rectangle with a color. */
  209.  
  210.     private int
  211. macpr_fill_rectangle (register gx_device *dev,
  212.                       int x, int y, int w, int h, gx_color_index color)
  213.  
  214. {
  215.     int    retVal = 0;
  216.  
  217.  
  218.     check_rect ();
  219.  
  220.     if (color != gx_no_color_index)
  221.     {
  222.         CWindowPtr    macPictWindow (gx_device *dev);
  223.         CWindowPtr    pictWin          = macPictWindow (dev);
  224.         Boolean        fClearPicture =
  225.                         ((CWindowPeek) pictWin)->port.picSave == (Handle) NULL &&
  226.                         x == 0 && y == 0 &&
  227.                         w == xdev->width && h == xdev->height;
  228.  
  229.  
  230.         BEGIN_DRAW_SPECIAL (fClearPicture)
  231.  
  232.             Rect        theRect;
  233.  
  234.  
  235.             if (gx_device_has_color (dev))
  236.             {
  237.                 RGBColor    rgbColor;
  238.  
  239.  
  240.                 rgbColor.red   = RED   (color);
  241.                 rgbColor.green = GREEN (color);
  242.                 rgbColor.blue  = BLUE  (color);
  243.                 RGBForeColor (&rgbColor);
  244.             }
  245.  
  246.             SetRect (&theRect, x, y, x + w, y + h);
  247.             PaintRect (&theRect);
  248.             ValidRect (&theRect);
  249.  
  250.         END_DRAW_SPECIAL
  251.  
  252.         /*...Handle page erasures...*/
  253.  
  254.         if (fClearPicture)
  255.             macClearPicture (dev);
  256.     }
  257.  
  258.     return retVal;
  259. }
  260.  
  261.  
  262. /* Copy a monochrome bitmap. */
  263.  
  264.     private int
  265. macpr_copy_mono (register gx_device *dev,
  266.                  const unsigned char *base, int sourcex, int raster, gx_bitmap_id id,
  267.                  int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  268.  
  269. {
  270.     int    retVal = 0;
  271.  
  272.  
  273.     check_rect ();
  274.  
  275.     BEGIN_DRAW
  276.  
  277.         Rect        srcRect, dstRect;
  278.         BitMap        srcBits, *dstBits;
  279.  
  280.  
  281.         if (gx_device_has_color (dev))
  282.         {
  283.             RGBColor    zeroColor;
  284.             RGBColor    oneColor;
  285.     
  286.  
  287.             zeroColor.red    = RED    (zero);
  288.             zeroColor.green = GREEN    (zero);
  289.             zeroColor.blue    = BLUE    (zero);
  290.             RGBBackColor (&zeroColor);
  291.     
  292.             oneColor.red    = RED    (one);
  293.             oneColor.green    = GREEN    (one);
  294.             oneColor.blue    = BLUE    (one);
  295.             RGBForeColor (&oneColor);
  296.         }
  297.     
  298.         SetRect (&srcRect, 0, 0, w, h);
  299.         SetRect (&dstRect, x, y, x + w, y + h);
  300.         srcBits.baseAddr      = (QDPtr) base;
  301.         srcBits.rowBytes      = raster;
  302.         srcBits.bounds.left      = srcRect.left;
  303.         srcBits.bounds.top    = srcRect.top;
  304.         srcBits.bounds.right  = srcRect.right;
  305.         srcBits.bounds.bottom = srcRect.bottom;
  306.         dstBits = &(((GrafPtr) windowPtr)->portBits);
  307.         CopyBits (&srcBits, dstBits, &srcRect, &dstRect, srcCopy, NULL);
  308.  
  309.     END_DRAW
  310.  
  311.     return retVal;
  312. }
  313.  
  314.  
  315. /* Copy a color bitmap. */
  316.  
  317.     private int
  318. macpr_copy_color (register gx_device *dev,
  319.                   const unsigned char *base, int sourcex, int raster,  gx_bitmap_id id,
  320.                   int x, int y, int w, int h)
  321.  
  322. {
  323.     int    retVal = 0;
  324.  
  325.  
  326.     check_rect ();
  327.  
  328.     return retVal;
  329. }
  330.  
  331.  
  332. /* Draw a line */
  333.  
  334.     private int
  335. macpr_draw_line (register gx_device *dev,
  336.                  int x0, int y0, int x1, int y1, gx_color_index color)
  337.  
  338. {
  339.     int    retVal = 0;
  340.  
  341.  
  342.     if (x0 == x1 && y0 == y1)
  343.         return 0;
  344.  
  345.     if (y1 < y0)
  346.     {
  347.         register int    tmp;
  348.  
  349.  
  350.         tmp = y1; y1 = y0; y0 = tmp;
  351.         tmp = x1; x1 = x0; x0 = tmp;
  352.     }
  353.  
  354.     if (color != gx_no_color_index)
  355.     {
  356.         BEGIN_DRAW
  357.  
  358.             if (gx_device_has_color (dev))
  359.             {
  360.                  RGBColor    rgbColor;
  361.  
  362.  
  363.                 rgbColor.red   = RED   (color);
  364.                 rgbColor.green = GREEN (color);
  365.                 rgbColor.blue  = BLUE  (color);
  366.                 RGBForeColor (&rgbColor);
  367.             }
  368.  
  369.             MoveTo (x0, y0);
  370.             LineTo (x1, y1);
  371.  
  372.         END_DRAW
  373.     }
  374.  
  375.     return retVal;
  376. }
  377.  
  378.  
  379. #define COLOR_TO_CHANNEL(s)        (s & 0xFF) * (ulong) gx_max_color_value / 255
  380.  
  381.  
  382.     private gx_color_index
  383. macpr_map_rgb_color (gx_device *dev, ushort r, ushort g, ushort b)
  384.  
  385. {
  386.     register gx_color_index index = (gx_color_index) 0;
  387.  
  388.  
  389.     index  = (r * 255L) / gx_max_color_value;
  390.     index <<= 8;
  391.     index |= (g * 255L) / gx_max_color_value;
  392.     index <<= 8;
  393.     index |= (b * 255L) / gx_max_color_value;
  394.  
  395.     return (index);
  396. }
  397.  
  398.  
  399.     private int
  400. macpr_map_color_rgb (gx_device *dev,
  401.                      register gx_color_index color, ushort prgb[3])
  402.  
  403. {
  404.     prgb[2] = COLOR_TO_CHANNEL (color);
  405.     color >>= 8;
  406.     prgb[1] = COLOR_TO_CHANNEL (color);
  407.     color >>= 8;
  408.     prgb[0] = COLOR_TO_CHANNEL (color);
  409.     color >>= 8;
  410.  
  411.     return 0;
  412. }
  413.  
  414.  
  415.     private int
  416. macpr_output_page (gx_device *dev, int num_copies, int flush)
  417.  
  418. {
  419.     macClosePicture (dev);
  420.  
  421.     return 0;    /* the page is "output" as it is drawn */
  422. }
  423.  
  424.  
  425.     private void
  426. macpr_get_initial_matrix (register gx_device *dev, register gs_matrix *pmat)
  427.  
  428. {
  429.     pmat->xx =  1;
  430.     pmat->xy =  0;
  431.     pmat->yx =  0;
  432.     pmat->yy = -1;
  433.     pmat->tx =  0;
  434.     pmat->ty = xdev->height;
  435. }
  436.  
  437.  
  438. enum
  439. {
  440.     iPostDoNothing,
  441.     iPostCmdWindow,
  442.     iPostScrollKeys
  443. };
  444.  
  445.  
  446. /*
  447.  *    DoKeyEvent -- handles keyboard events
  448.  *
  449.  */
  450.  
  451.     static void
  452. DoKeyEvent (EventRecord *pEvent)
  453.  
  454. {
  455.     gx_device  *dev = (gx_device *) &gs_mac_device;
  456.  
  457.  
  458.     if (pEvent->modifiers & cmdKey)
  459.     {
  460.         char            aChar        = pEvent->message & charCodeMask;
  461.         short            flashMenuID = 0;
  462.         char            iPostAction = iPostDoNothing;
  463.  
  464.  
  465.         if ('a' <= aChar && aChar <= 'z')
  466.             aChar += 'A' - 'a';
  467.  
  468.         switch (aChar)
  469.         {
  470.             case 'P':
  471.             {
  472.                 void    doPrint (gx_device *dev);
  473.  
  474.  
  475.                 FlashMenuBar (MacGSMenuID);
  476.                 flashMenuID = MacGSMenuID;
  477.                 doPrint (dev);
  478.  
  479.                 break;
  480.             }
  481.  
  482.             case 'Q':
  483.             {
  484.                 macClosePicture (dev);
  485.                 break;
  486.             }
  487.         }
  488.  
  489.         if (flashMenuID /* != 0 */)
  490.         {
  491.             FlashMenuBar (flashMenuID);
  492.             if (flashMenuID > 0)
  493.                 FlashMenuBar (flashMenuID);
  494.             HiliteMenu (0);
  495.         }
  496.  
  497.         switch (iPostAction)
  498.         {
  499.             case iPostCmdWindow:
  500.                 SendBehind (FrontWindow (), (WindowPtr) NULL);
  501.                 break;
  502.  
  503.             default:
  504.                 break;
  505.         }
  506.     }
  507. }
  508.  
  509.  
  510. extern Boolean    gDoCheckInterrupts;
  511.  
  512.  
  513.     static void
  514. DoOpenFilePre (void)
  515.  
  516. {
  517.     SetDoMacOpenFile (TRUE);
  518.  
  519.     gDoCheckInterrupts = TRUE;
  520. }
  521.  
  522.  
  523.     static void
  524. DoOpenFilePost (void)
  525.  
  526. {
  527.     gDoCheckInterrupts = FALSE;
  528.  
  529.     SetDoMacOpenFile (FALSE);
  530. }
  531.  
  532.  
  533.     static char
  534. DoOptionsProc (short actionValue)
  535.  
  536. {
  537.     char    retVal = TRUE;
  538.  
  539.  
  540.     switch (actionValue)
  541.     {
  542.         case iInitDialogAction:
  543.             retVal = FALSE;
  544.             break;
  545.  
  546.         case iDoDialogAction:
  547.             break;
  548.  
  549.         case iCommitAction:
  550.             (void) doRunIndString (iShowpageNoPrompt);
  551.             break;
  552.  
  553.         case iRetractAction:
  554.             break;
  555.  
  556.         default:
  557.             break;
  558.     }
  559.  
  560.     return retVal;
  561. }
  562.